import time

from selenium import webdriver

driver = webdriver.Chrome()
url = ("file:///C:/Users/Mr%20MING/PycharmProjects/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/learn-git/%E4%B8%89%E7%A7%8D%E5%88%87%E6%8D%A2/alert%E5%88%87%E6%8D%A2%E4%BE%8B%E5%AD%90.html")
driver.get(url)

h2 = driver.find_element("xpath", '//h2')
h2.click()

# 切换到alert，点击确定，没有括号，也不需要传参
my_alert = driver.switch_to.alert

time.sleep(2)
# 确定
my_alert.accept()
# 取消
# my_alert.dismiss()

# driver.find_element("xpath", "//h2")